
<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of locationController
 *
 * @author luigi.pastore
 * @created  11-mag-2011
 */
class LocationController extends Classes_Controller
{
    public function init()
    {
	$this->model_location	= new Classes_Location();
	$this->model_province	= new Classes_Province();
	
	$this->contact_roles		= $this->application->getResource( 'ContactRoles' );
	$this->layout->contact_roles	= $this->contact_roles;
	
	$this->layout->province = $this->model_province->fetchAll();
    }
    
    public function indexAction()
    {
	$this->layout->content = array( 'script' => 'location/index' );
    }
    
    public function nuovoAction()
    {
	if( $_POST['salva'] AND $_POST['salva'] !== '' )
	{
	    $this->layout->data = $_POST;

	    // Validiamo server-side...
	    $errorMsg = $this->checkFormData( $_POST );

	    if( !$errorMsg )
	    {
		# Mapping...
		$data = array(
		    'location_name'		    => $_POST['location_name'],
		    'location_desc'		    => $_POST['location_desc'],
		    'location_city'		    => $_POST['location_city'],
		    'location_cap'		    => $_POST['location_cap'],
		    'location_loc'		    => $_POST['location_loc'],
		    'location_street'		    => $_POST['location_street'],
		    'location_street_number'	    => $_POST['location_street_number'],
		    'location_province_id'	    => $_POST['location_province_id'],
		    'location_createdby'	    => $this->application->auth->user['user_id'],
		);
		
		$model = new Classes_Location( $data );

		if( is_numeric( $location_id = $model->save()->location_id ) )
		{
		    // Rimanda alla pagina di associazione dei contatti...
		    header( 'Location: /admin/location/contatti/id/' . $location_id );
		}
		else
		{
		    trigger_error( 'Errore Inserimento Locale' );
		}
	    }
	}

	$this->layout->errorMsg = $errorMsg;
	$this->layout->content	= array( 'script' => 'location/nuovo' );
    }
    
    public function editAction()
    {
	// Edit
	if( !is_null( $this->getParam( 'id' ) ) )
	{
	    // Fetch
	    $location = $this->model_location->fetchById( array( $this->getParam( 'id' ) ) );

	    // Se non c'è il post lancia l'errore
	    if( !is_array( $location ) )
	    {
		trigger_error( 'Errore Edit location', E_USER_WARNING );
	    }
	    else
	    {
		# Assegniamo i dati al layout
		$location = array_shift( $location );
		$this->layout->data = $location;
	    }
	}
	
	if( $_POST['salva'] AND $_POST['salva'] !== '' )
	{
	    $this->layout->data = $_POST;

	    // Validiamo server-side...
	    $errorMsg = $this->checkFormData( $_POST );
	    
	    if( !$errorMsg )
	    {
		# Mapping...
		$data = array(
		    'location_id'		    => $location['location_id'],
		    'location_name'		    => $_POST['location_name'],
		    'location_desc'		    => $_POST['location_desc'],
		    'location_city'		    => $_POST['location_city'],
		    'location_cap'		    => $_POST['location_cap'],
		    'location_loc'		    => $_POST['location_loc'],
		    'location_street'		    => $_POST['location_street'],
		    'location_street_number'	    => $_POST['location_street_number'],
		    'location_province_id'	    => $_POST['location_province_id'],
		    'location_modifiedby'	    => $this->application->auth->user['user_id'],
		);

		$model = new Classes_Location( $data );

		if( is_numeric( $location_id = $model->save()->location_id ) )
		{
		    // Rimanda alla pagina di conferma...
		    $this->layout->forward = 'location/';

		    return $this->layout->content = array( 'script' => 'completed' );
		}
		else
		{
		    trigger_error( 'Errore Edit Locale' );
		}
	    }
	}

	$this->layout->errorMsg = $errorMsg;
	$this->layout->content = array( 'script' => 'location/edit' );	
    }
    
    public function archivioAction()
    {
	$this->layout->locations = $this->model_location->fetchAll();
	$this->layout->content = array( 'script' => 'location/archivio' );
    }    
    
    public function vediAction()
    {
	if( is_null( $this->getParam( 'id' ) ) )
	{
	    trigger_error( 'Errore Vedi Locale' );
	}
	
	// Fetch
	$location = $this->model_location->fetchById( array( $this->getParam( 'id' ) ) );

	// Se non c'è lancia l'errore
	if( !is_array( $location ) )
	{
	    trigger_error( 'Errore Vedi Locale', E_USER_WARNING );
	}
	else
	{
	    # Assegniamo i dati al layout
	    $this->layout->data = array_shift( $location );
	}
	
	$this->layout->content	= array( 'script' => 'location/vedi' );		
    }
    
    public function contattiAction()
    {
        if( is_null( $this->getParam( 'id' ) ) )
	{
	    trigger_error( 'Errore Vedi Locale' );
	}
	
	// Fetch
	$location = array_shift( $this->model_location->fetchById( array( $this->getParam( 'id' ) ) ) );

        # Assegniamo i dati al layout
        $this->layout->data =  $location;
        
	// Salviamo in sessione i valori precedenti, che verranno utilizzati
	// da AjaxController->selectRemContactLocation()
	$_SESSION['ContactLocation'] = count( $location['location_contacts'] )  ? array_keys( $location['location_contacts'] ) : array( );
	
        if( $_POST )
	{
            $roles = array();
	    
	    foreach( $_POST AS $key => $value )
	    {
		if( substr( $key, 0, 8 ) == 'contact_' )
		{
		    // Mapping...
		    $roles[] = array( 'fab_locations_location_id' => $location['location_id'], 'fab_contacts_contact_id' => substr( $key, 8 ), 'fab_contacts_role' => $value );
		}
	    }
	    
	    if( !$errorMsg )
	    {
		$model = new Classes_ContactLocation( array( 'fab_locations_location_id' => $location['location_id'] ) );
		
		if(  $model->save( $roles ) === TRUE )
		{
		    // Rimanda alla pagina di conferma e cancella file temporanei...
		    $this->layout->forward = 'location/archivio';

		    return $this->layout->content = array( 'script' => 'completed' );
		}
		else
		{
		    trigger_error( 'Errore Edit Contatto' );
		}
	    }
        }
        
	$this->layout->errorMsg = $errorMsg;
	$this->layout->content	= array( 'script' => 'location/location-contatti' );	
    }


    private function checkFormData( $data )
    {
	if( !filter_var( $data['location_name'], FILTER_SANITIZE_STRING ) )
	    $errorMsg['location_name'] = ERR_EMPTY;

	if( $data['location_desc'] != '' AND !filter_var( $data['location_desc'], FILTER_SANITIZE_STRING ) )
	    $errorMsg['location_desc'] = ERR_EMPTY;

	if( !filter_var( $data['location_street'], FILTER_SANITIZE_STRING ) OR !filter_var( $data['location_city'], FILTER_SANITIZE_STRING ) )
	    $errorMsg['location_address'] = ERR_EMPTY;

	if( ( strlen( $data['location_cap'] ) != '' ) AND ( strlen( $data['location_cap'] ) != 5 OR ( !filter_var( $data['location_cap'], FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 0, 'max_range' => 98100 ) ) ) ) ) )
	    $errorMsg['location_address'] = sprintf( ERR_FORMAT, 'CAP' );

	return $errorMsg ? $errorMsg : false;
    }
}
